<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        .layui-table-checked {
            background-color: #9e9e9e14;
        }

        .layui-table-checked.layui-table-hover {
            background-color: #9e9e9e14 !important;
        }
    </style>
</head>
<body>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="electronArchives-table" lay-filter="electronArchives-table"></table>
    </div>
</div>
<button type="button" class="layui-btn" id="uploadFile" lay-options="{accept: 'file'}" style="display: none">
    <i class="layui-icon layui-icon-upload"></i>
    上传文件
</button>
<button type="button" class="layui-btn" id="mergeFiles" lay-options="{accept: 'file'}" style="display: none">
    <i class="layui-icon layui-icon-upload"></i>
    合并文件
</button>
<script type="text/html" id="electronArchives-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        上传电子档案
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除档案
    </button>
</script>
<!--<script type="text/html" id="user-bar">-->
<!--    <button plain class="pear-btn pear-btn-primary pear-btn-xs" lay-event="show">查看档案</button>&nbsp;&nbsp;-->
<!--    <button plain class="pear-btn pear-btn-warming  pear-btn-xs" lay-event="merge">合并档案</button>&nbsp;&nbsp;-->
<!--    <button plain class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除档案</button>-->
<!--</script>-->

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script th:inline="javascript">
    var archivesId = [[${archivesId}]];
    layui.use(['table', 'form', 'jquery', 'common', 'upload'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let upload = layui.upload;
        let cols = [
            [
                {type: 'checkbox'},
                {title: '文件名称', field: 'upload_file_name', align: 'center'},
                {title: '文件大小', field: 'file_size', align: 'center'},
                {title: '上传日期', field: 'create_time', align: 'center'},
                {
                    title: '操作',align: 'center', minWidth: 310, templet: function (d) {
                        let splitName = d.upload_file_name.split(".");
                        let suffixName = splitName[splitName.length - 1];
                        if (suffixName=== "pdf") {
                            return ' <button plain class="pear-btn pear-btn-primary pear-btn-xs" lay-event="show">查看档案</button>' +
                                '<button plain class="pear-btn pear-btn-warming  pear-btn-xs" lay-event="merge">合并档案</button>' +
                                '<button plain class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除档案</button>';
                        }else{
                            return ' <button plain class="pear-btn pear-btn-primary pear-btn-xs" lay-event="show">查看档案</button>' +
                                '<button plain class="pear-btn pear-btn-danger pear-btn-xs" lay-event="remove">删除档案</button>';
                        }
                    }
                }
            ]
        ]
        table.render({
            elem: '#electronArchives-table',
            url: '/electron/archives/table/list',
            page: true,
            where: {archivesId: archivesId},
            cols: cols,
            skin: 'line',
            toolbar: '#electronArchives-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'electronArchives',
        });
        table.on('toolbar(electronArchives-table)', function (obj) {
            if (obj.event === 'add') {
                $("#uploadFile").click();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });
        // 刷新
        window.refresh = function (param) {
            table.reload('electronArchives');
        }
        // 新增电子档案
        var inst = upload.render({
            elem: "#uploadFile"
            , url: '/archives/upload/electron/file'
            , accept: 'file'//允许上传的文件类型
            , exts: 'pdf|jpg|png'//只允许上传jpg和png图片格式
            , data: {archivesId: archivesId}
            , before: function (obj) {
                layer.load(); //上传loading
            }
            , done: function (res) {
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传成功');
                table.reload('electronArchives');
                inst.reload()
            }
            , error: function (index, upload) {
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传失败，请重试');
            }
        });
        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些档案', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/electron/archives/delete/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            if (result.data != null) {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.admin.refreshThis()
                            } else {
                                layer.msg(result.message, {
                                    icon: 1,
                                    time: 2000
                                }, function () {
                                    table.reload("electronArchives");
                                });
                            }
                        } else {
                            layer.msg(result.message, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }
                })
            });
        }

        table.on('tool(electronArchives-table)', function (obj) {
            if (obj.event === 'show') {
                window.show(obj);
            } else if (obj.event === 'merge') {
                lineID = obj.data.id;
                $("#mergeFiles").click();
            } else if (obj.event === 'remove') {
                window.remove(obj);
            }
        });
        //合并文件
        var lineID = null;
        var mergeFiles = upload.render({
            elem: "#mergeFiles"
            , url: '/electron/archives/upload/electron/file'
            , accept: 'file'//允许上传的文件类型
            , exts: 'pdf'
            , data: {archivesId: archivesId}
            , before: function (obj) {
                layer.load(); //上传loading
            }
            , done: function (res) {
                layer.msg('上传成功,正在合并');
                // table.reload('electronArchives');
                // mergeFiles.reload()
                console.log(res)
                $.ajax({
                    url: '/electron/archives/merge/pdf',
                    data: JSON.stringify({
                        "archivesId": archivesId,
                        "savePath": res.savePath,
                        "lineID": lineID,
                        "fileName": res.fileName
                    }),
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    type: 'post',
                    success: function (result) {
                        layer.closeAll('loading');
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 1800}, function () {
                                table.reload("electronArchives");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 1800});
                        }
                    }
                })
            }
            , error: function (index, upload) {
                layer.closeAll('loading'); //关闭loading
                layer.msg('上传失败，请重试');
            }
        });
        // 查看电子档案
        window.show = function (obj) {
            let splitName = obj.data.upload_file_name.split(".");
            let suffixName = splitName[splitName.length - 1];
            if (suffixName === "pdf") {
                parent.layer.open({
                    type: 2,
                    title: '查看电子档案',
                    shadeClose: true,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['1000px', '700px'],
                    content: obj.data.electron_file,
                    success: function (layero, index) {
                        parent.layer.full(index); // 最大化
                    }
                });
            } else if (suffixName === "jpg" || suffixName === "png") {
                parent.layer.photos({
                    photos: {
                        "title": "Photos Demo",
                        "start": 0,
                        "data": [
                            {
                                "alt": obj.data.upload_file_name,
                                "pid": 5,
                                "src": obj.data.electron_file,
                            }
                        ]
                    },
                    footer: true, // 是否显示底部栏 --- 2.8.16+
                });
            } else {
                layer.msg("暂不支持该格式文件预览");
            }
        }
        // 单行删除
        window.remove = function (obj) {
            layer.confirm('确定要删除该档案', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/electron/archives/delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            if (result.data != null) {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.layui.admin.refreshThis()
                            } else {
                                layer.msg(result.message, {
                                    icon: 1,
                                    time: 2000
                                }, function () {
                                    obj.del();
                                    table.reload("electronArchives");
                                });
                            }

                        } else {
                            layer.msg(result.message, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }
                })
            });
        }
    })


</script>
</body>
</html>